package com.csoa.community.adapter;

import java.util.List;

import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.csoa.community.R;
import com.csoa.community.activity.OfficialDocActivity;
import com.csoa.community.bean.OfficialDocBean;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.AndroidUtils;

public class OffDocAdapter extends BaseAdapter {

	private OfficialDocActivity activity;
	private List<OfficialDocBean> officialDocs;

	public OffDocAdapter(OfficialDocActivity activity,
			List<OfficialDocBean> officialDocs) {
		this.activity = activity;
		this.officialDocs = officialDocs;
	}

	@Override
	public int getCount() {
		return officialDocs == null ? 0 : officialDocs.size();
	}

	@Override
	public OfficialDocBean getItem(int arg0) {
		return officialDocs == null ? null : officialDocs.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = null;
		ViewHolder holder = null;
		if (null == convertView) {
			holder = new ViewHolder();
			view = AndroidUtils.inflateView(activity,
					R.layout.adapter_official_tobe_read_item, null);
			holder.content = (TextView) view
					.findViewById(R.id.adapterOfficialDocTV);
			holder.issued = (Button) view
					.findViewById(R.id.adapterOfficialIssuedBtn);
			holder.update = (Button) view
					.findViewById(R.id.adapterOfficialUpdateBtn);
			holder.time = (TextView) view
					.findViewById(R.id.adapterOfficialTimeTV);
			view.setTag(holder);
		} else {
			view = convertView;
			holder = (ViewHolder) view.getTag();
		}

		final OfficialDocBean offDoc = getItem(position);
		if (offDoc != null) {
			holder.content.setText(offDoc.title);
			holder.time.setText(offDoc.sendDate);
			if(offDoc.ImportGrade.equals("紧急")) {
				holder.content.setTextColor(Color.RED);
				holder.time.setTextColor(Color.RED);
				holder.content.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
			}
			boolean isRead = offDoc.isRead;
			if(!isRead) {
				holder.issued.setVisibility(View.VISIBLE);
				holder.update.setVisibility(View.INVISIBLE);
			}else {
				holder.issued.setVisibility(View.INVISIBLE);
				holder.update.setVisibility(View.VISIBLE);
			}
			holder.issued.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
//					Intent intent = new Intent(activity,
//							OffDocDetailActivity.class);
//					intent.putExtra("ID", offDoc.ID);
//					intent.putExtra("RID", offDoc.RID);
//					intent.putExtra(GlobalVars.READ_TYPE, GlobalVars.READ_TYPE_TO_BE_READ);
//					intent.putExtra("issued", true);
//					activity.startActivity(intent);
					activity.buildRequestMeeting(offDoc.ID, offDoc.RID,GlobalVars.ISSUED,true,GlobalVars.READ_TYPE_TO_BE_READ,GlobalVars.TO_BE_READ,position);
				}
			});
			holder.update.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
//					Intent intent = new Intent(activity,
//							OffDocDetailActivity.class);
//					intent.putExtra("ID", offDoc.ID);
//					intent.putExtra("RID", offDoc.RID);
//					intent.putExtra(GlobalVars.READ_TYPE, GlobalVars.READ_TYPE_HAVE_READ);
//					intent.putExtra("isupdate", true);
//					activity.startActivity(intent);
					
					activity.buildRequestMeeting(offDoc.ID, offDoc.RID,GlobalVars.ISUPDATE,true,GlobalVars.READ_TYPE_HAVE_READ,GlobalVars.HAVE_READ,position);
				}
			});

		}
		return view;
	}

	public final class ViewHolder {

		public TextView content;
		public Button issued;
		public Button update;
		public TextView time;

	}

}
